<?PHP
    $pp_path="..";
    include_once ($pp_path."/common.php");
    include_once ($pp['lib_path']."/gcm.lib.php");

    $gcm=new gcm;

    $query="SELECT r_idx, p_code, r_contents FROM gcm_result WHERE r_status='2' AND r_regdate <= ".time();
    $result=$mysql->query($query);

    while($row=$mysql->fetch_array($result)) {
        $key_query="SELECT p_apikey FROM gcm_project WHERE p_code={$row['p_code']}";
        $key=$mysql->fetch($key_query);

        $device_query="SELECT d_code FROM gcm_device WHERE p_code={$row['p_code']}";
        $device_result=$mysql->query($device_query);
        $device=array();
        while($devicerow=$mysql->fetch_array($device_result)) {
            array_push($device, $devicerow['d_code']);
        }

        $post_result=$gcm->post($device, $key['p_apikey'], $row['r_contents']);
        $decode_result=json_decode($post_result);

        $query="UPDATE gcm_result SET r_status='1', r_multicastid='".$decode_result->{'multicast_id'}."', r_success='".$decode_result->{'success'}."', r_failure='".$decode_result->{'failure'}."', r_canonical_ids='".$decode_result->{'canonical_ids'}."', r_result='".json_encode($decode_result->{'results'})."', r_resultdate=UNIX_TIMESTAMP() WHERE r_idx={$row['r_idx']}";
        $mysql->query($query);
    }
?>